package com.ceba.test.modules.log.helper;

import com.ceba.cebautils.DateUtil;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.web.response.WebServiceResponseInfo;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.enums.LogOpTypeEnum;
import com.ceba.test.base.enums.LogTypeEnum;
import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.modules.log.logicService.LogLogicService;

import configs.context.SpringBeans;

public class RestLogHelper {

	private static Logger logger = LoggerFactory.getLogger(RestLogHelper.class);

	private static LogLogicService logLogicService = SpringBeans.getLogLogicService("logLogicService");

	/*
	 * 调用接口开始时，打印日志
	 */
	public static void fnStartDebug(String threadName, String interfaceName) {
		logger.debug(threadName + "调用" + interfaceName + ",调用时间=" + DateUtil.getNowTime(DateUtil.YYYYMMDDHHMMSSS));
		logLogicService.addLog(LogTypeEnum.INTERFACE_LOGTYPE.getType(), LogOpTypeEnum.INIT_OPYPE.getType(),
				threadName + ResourceUtils.getMessage("MODULES_CALL") + interfaceName + ","+ResourceUtils.getMessage("MODULES_CALL_TIME")+"=" + DateUtil.getNowTime(DateUtil.YYYYMMDDHHMMSSS), "","");

	}

	public static void fnStartDebug(String threadName, String interfaceName, int logType, int logOpType) {
		logger.debug(threadName + "调用" + interfaceName + ",调用时间=" + DateUtil.getNowTime(DateUtil.YYYYMMDDHHMMSSS));
		logLogicService.addLog(logType, logOpType,
				threadName + ResourceUtils.getMessage("MODULES_CALL") + interfaceName + ","+ResourceUtils.getMessage("MODULES_CALL_TIME")+"=" + DateUtil.getNowTime(DateUtil.YYYYMMDDHHMMSSS), "","");

	}

	/*
	 * 调用接口，输入参数时，打印日志
	 */
	public static void fnPrintParamDebug(String threadName, String interfaceName, String json, String fileName) {
		if (StringUtil.isEmptyString(fileName)) {
			logger.debug(threadName + "调用" + interfaceName + ",json=" + json + "; ");
		} else {
			logger.debug(threadName + "调用" + interfaceName + ",json=" + json + "; dhName=" + fileName);
		}
		logLogicService.addLog(LogTypeEnum.INTERFACE_LOGTYPE.getType(), LogOpTypeEnum.SEACH_OPYPE.getType(),
				threadName + ResourceUtils.getMessage("MODULES_CALL") + interfaceName + ",json=" + json + "; dhName=" + fileName, "","");
	}

	/*
	 * 调用目录接口，输入参数时，打印日志
	 */
	public static void fnPrintDirParamDebug(String threadName, String interfaceName, String dirParam) {
		logger.debug(threadName + "调用" + interfaceName + ",dirParam=" + dirParam);
		logLogicService.addLog(LogTypeEnum.INTERFACE_LOGTYPE.getType(), LogOpTypeEnum.SEACH_OPYPE.getType(),
				threadName + ResourceUtils.getMessage("MODULES_CALL") + interfaceName + ",dirParam=" + dirParam, "","");
	}

	/*
	 * 调用接口返回时，打印日志
	 */
	public static void fnEndDebug(String threadName, String interfaceName, String result) {
		logger.debug(threadName + "调用" + interfaceName + ",返回result=" + result);
		logLogicService.addLog(LogTypeEnum.INTERFACE_LOGTYPE.getType(), LogOpTypeEnum.CLOSE_OPYPE.getType(),
				threadName + ResourceUtils.getMessage("MODULES_CALL") + interfaceName + ","+ResourceUtils.getMessage("IDS_RETURN")+"result=" + result, "","");
	}

	public static void fnEndDebug(String threadName, String interfaceName, String result, int logType, int logOpType) {
		logger.debug(threadName + "调用" + interfaceName + ",返回result=" + result);
		logLogicService.addLog(logType, logOpType, threadName + ResourceUtils.getMessage("MODULES_CALL") + interfaceName + ",返回result=" + result, "","");
	}

	/*
	 * 调用接口返回时，打印日志
	 */
	public static void fnEndDebug(String threadName, String interfaceName, WebServiceResponseInfo r) {
		logger.debug(threadName + "调用" + interfaceName + ",返回result=" + new JSONObject(r).toString());
		logLogicService.addLog(LogTypeEnum.INTERFACE_LOGTYPE.getType(), LogOpTypeEnum.CLOSE_OPYPE.getType(),
				threadName + ResourceUtils.getMessage("MODULES_CALL") + interfaceName + ","+ResourceUtils.getMessage("IDS_RETURN")+"result=" + new JSONObject(r).toString(), "","");
	}


}
